Workport.Google.drive = (function() {
    var drive,
    debug = false;
    return{
        init:function(){
            drive = gapi.client.drive;
        }, 
        getProfile: function(args, callback) {
            drive.users.getProfile(args).execute(callback);
        },
        files:{
            list:function(callback){
                var retrievePageOfFiles = function(request, result) {
                    request.execute(function(resp) {
                        if(resp.error){
                            $('#drive_min_panel_div').empty();
                            $('#drive_min_panel_div').append('<div id="timeout_message" class="center">'+resp.error.message+'</div>');
                            $('#drive').find('.common_loading_mask').hide();
                            $('#drive').find('.common_loading_conent').hide();
                            $('.drive_panel_functions').hide();
                            return
                        }
                        result = result.concat(resp.items);
                        var nextPageToken = resp.nextPageToken;
                        if (nextPageToken) {
                            request = gapi.client.drive.files.list({
                                'pageToken': nextPageToken                                
                            });
                            retrievePageOfFiles(request, result);
                        } else {
                            callback(result);
                        }
                    });
                }
                var initialRequest = gapi.client.drive.files.list();
                retrievePageOfFiles(initialRequest, []);
            },
            listtrash:function(callback){
                var retrievePageOfFiles = function(request, result) {
                    request.execute(function(resp) {
                        if(resp.error){
                            $('#drive_min_panel_div').empty();
                            $('#drive_min_panel_div').append('<div id="timeout_message" class="center">'+resp.error.message+'</div>');
                            $('#drive').find('.common_loading_mask').hide();
                            $('#drive').find('.common_loading_conent').hide();
                            $('.drive_panel_functions').hide();
                            return
                        }
                        result = result.concat(resp.items);
                        var nextPageToken = resp.nextPageToken;
                        if (nextPageToken) {
                            request = gapi.client.drive.files.list({
                                'pageToken': nextPageToken,
                                'q':'trashed = true'    
                            });
                            retrievePageOfFiles(request, result);
                        } else {
                            callback(result);
                        }
                    });
                }
                var initialRequest = gapi.client.drive.files.list();
                retrievePageOfFiles(initialRequest, []);
            },
            listRoot:function(param, callback){
                var request = gapi.client.drive.files.list(
                    $.extend({
                        'maxResults':30,
                        'q':'trashed=false'
                    }, param));
                request.execute(callback);
            },
            listFull:function(param, callback){
                var self = this, list = [], flag = 0;
                this.listRoot(param, function(resp){
                    if(resp.error){
                        $('#drive_min_panel_div').empty();
                        $('#drive_min_panel_div').append('<div id="timeout_message" class="center">'+resp.error.message+'</div>');
                        $('#drive').find('.common_loading_mask').hide();
                        $('#drive').find('.common_loading_conent').hide();
                        $('.drive_panel_functions').hide();
                        return
                    }
                    var size = resp.items.length;
                    if(size == 0){
                        callback(list, resp.nextPageToken);
                    }
                    for(var i= 0;i<size;i++){
                        var index = i;
                        self.get(resp.items[i].id, function(msg){
                            flag++;
                            list.push(msg);
                            if(flag == size){
                                callback(list, resp.nextPageToken);
                            }
                        });
                    }
                });
            },
            listfolderfiles:function(param,folderid,callback){
                var request = gapi.client.drive.children.list(
                    $.extend({
                        'folderId' : folderid,
                        'maxResults':30
                    }, param));
                request.execute(callback);
            },
            searchFullText:function(param,callback){
                var request = gapi.client.drive.files.list(
                    $.extend({

                    }, param));
                request.execute(callback);
            },
            listFullfolderfiles:function(param,folderid,callback){
                var self = this, list = [], flag = 0;
                this.listfolderfiles(param,folderid,function(resp){ 
                    if(resp.error){
                        $('#drive_min_panel_div').empty();
                        $('#drive_min_panel_div').append('<div id="timeout_message" class="center">'+resp.error.message+'</div>');
                        $('#drive').find('.common_loading_mask').hide();
                        $('#drive').find('.common_loading_conent').hide();
                        $('.drive_panel_functions').hide();
                        return
                    }
                    var size = resp.items.length;
                    if(size == 0){
                        callback(list, resp.nextPageToken);
                    }
                    for(var i= 0;i<size;i++){
                        
                        var index = i;
                        self.get(resp.items[i].id, function(msg){
                            flag++;
                            list.push(msg);
                            if(flag == size){
                                callback(list, resp.nextPageToken);
                            }
                        });
                    }
                });
            },
            delete:function(fileId,callback){
                var request = gapi.client.drive.files.delete({
                    'fileId': fileId
                });
                request.execute(function(resp) { 
                    callback(resp);
                });
            },
            get:function(fileId,callback){
                var request = gapi.client.drive.files.get({
                    'fileId': fileId
                }).execute(callback);
            },
            trash:function(fileId,callback){
                var request = gapi.client.drive.files.trash({
                    'fileId': fileId
                });
                request.execute(function(resp){
                    callback(resp)
                });
            },
            retrievePermissions:function(fileId,permissionId,callback){
                var request = gapi.client.drive.permissions.delete({
                    'fileId': fileId,
                    'permissionId': permissionId
                });
              request.execute(function(resp) {
                callback(resp);
              });
            },
            printAbout:function(callback){
                var request = gapi.client.drive.about.get();
                request.execute(function(resp) {
                     callback(resp);
                });
            },
            listsort:function(array){
                var list = [];
                var folder = [];
                var file = [];
                for(m in array){
                    if(array[m].mimeType == 'application/vnd.google-apps.folder'){
                        folder.push(array[m])
                    }else{
                        file.push(array[m])
                    }
                }
                for(i in folder){
                    list.push(folder[i])
                }
                for(i in file){
                    list.push(file[i])
                }
                return list;
            }
        }
    }
}());